package com.ad.pet.app;


import com.ad.pet.Logger;

/**
 * Created by z on.
 * Des:UseCase执行 者
 * Date: 2017/5/16
 */

public class UseCaseHandler {
    private static final String TAG = "UseCaseHandler";
    private static UseCaseHandler INSTANCE;

    public <T extends UseCase.RequestValues, R extends UseCase.ResponseValue>
    void execute(final UseCase<T, R> useCase, T values, UseCase.UseCaseCallback<R> callback) {

        if (useCase == null) {
            Logger.e(TAG, "useCase is null");
            return;
        }
        if (callback == null) {
            Logger.e(TAG, "callback is null");
            return;
        }

        useCase.setRequestValues(values);
        useCase.setUseCaseCallback(callback);
        useCase.getUseCaseCallback().onStart();
        useCase.run();
    }

    public <T extends UseCase.RequestValues, R extends UseCase.ResponseValue> void stopTask(final UseCase<T, R> useCase) {
        if (useCase != null)
            useCase.stop();
    }

    public static UseCaseHandler getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new UseCaseHandler();
        }
        return INSTANCE;
    }
}
